package banner

import (
	"context"
	"group-buy/group_buy/internal/models"

	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListBannerLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewListBannerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListBannerLogic {
	return &ListBannerLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListBannerLogic) ListBanner() (resp *types.ListBannerRsp, err error) {
	data := make([]*models.Banner, 0)
	err = l.svcCtx.Engine.OrderBy("sort").Find(&data)
	if err != nil {
		return nil, err
	}
	list := make([]*types.Banner, len(data), len(data))
	for i, _ := range data {
		list[i] = &types.Banner{
			Id:    data[i].Id,
			Title: data[i].Title,
			Path:  data[i].Path,
			Url:   data[i].Url,
			Sort:  data[i].Sort,
		}
	}

	return &types.ListBannerRsp{
		List: list,
	}, nil
}
